package com.yenlien.traceyun.business.service.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-21
 * @description:
 **/

@Service
public class RestService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 请求json
     *
     * @param param
     * @return
     */
    public JSONObject postJson(String url, JSONObject param) {
        JSONObject result = restTemplate.postForObject(url, param, JSONObject.class);
        return result;
    }

    /**
     * 请求json
     *
     * @param param
     * @return
     */
    public JSONObject postJson(String url, JSONArray param) {
        JSONObject result = restTemplate.postForObject(url, param, JSONObject.class);
        return result;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public JSONObject getJson(String url, Map<String, String> param) {
        JSONObject result = restTemplate.getForObject(url, JSONObject.class, param);
        return result;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public JSONObject getJson(String url) {
        JSONObject result = restTemplate.getForObject(url, JSONObject.class);
        return result;
    }

    public JSONArray getJsonArray(String url) {
        JSONArray result = restTemplate.getForObject(url, JSONArray.class);
        return result;
    }

    /**
     * 请求json设置请求头
     *
     * @param url
     * @param param
     * @return
     */
    public JSONObject postEntity(String url, JSONObject param) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(param.toString(), headers);
        String s = restTemplate.postForEntity(url, formEntity, String.class).getBody();
        return JSONObject.parseObject(s);
    }

    /**
     * 上传表单
     *
     * @param url
     * @param values
     * @param files
     * @return
     */
    public JSONObject postForm(String url, Map<String, Object> values, List<MultipartFile> files) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        if (values != null && values.size() > 0) {
            for (Map.Entry<String, Object> entry : values.entrySet()) {
                form.add(entry.getKey(), entry.getValue());
            }
        }
        if (files != null && files.size() > 0) {
            for (MultipartFile file : files) {
                form.add("files", file.getResource());
            }
        }
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(form, headers);
        String s = restTemplate.postForEntity(url, httpEntity, String.class).getBody();//发送
        return JSONObject.parseObject(s);
    }

    /**
     * 上传表单
     *
     * @param url
     * @param files
     * @return
     */
    public String postFile(String url, List<MultipartFile> files) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        if (files != null && files.size() > 0) {
            for (MultipartFile file : files) {
                form.add("files", file.getResource());
            }
        }
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(form, headers);
        String s = restTemplate.postForEntity(url, httpEntity, String.class).getBody();//发送
        return s;
    }

    /**
     * 下载
     *
     * @param url
     * @return
     */
    public byte[] downFile(String url) {
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
        ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, byte[].class);
        return response.getBody();
    }
}
